package com.hgyc.mom.wms.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.wms.entity.WmsStock;
import com.hgyc.mom.wms.vo.WmsStockParam;
import com.hgyc.mom.wms.service.WmsStockService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 库存Controller
 *
 * @author fwj
 * @since 2025-06-26
 */
@Tag(name = "库存控制器", description = "库存控制器")
@RestController
@RequestMapping("/wms/stock")
public class WmsStockController {
    @Autowired
    private WmsStockService wmsStockService;

    @Operation(summary = "查询库存列表", description = "查询库存列表")
    @PostMapping("/pageList")
    public Result<IPage<WmsStock>> pageList(@Parameter(description = "查询参数") @RequestBody WmsStockParam wmsStockParam) {
        Page<WmsStock> page = new Page<>(wmsStockParam.getPage(), wmsStockParam.getPageSize());
        WmsStock wmsStock = new WmsStock();
        BeanUtils.copyProperties(wmsStockParam, wmsStock);
        QueryWrapper<WmsStock> wrapper = QueryWrapperUtil.build(wmsStock);
        IPage<WmsStock> list = wmsStockService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取库存详细信息", description = "获取库存详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "库存ID") @PathVariable("id") Long id) {
        return Result.success(wmsStockService.getById(id));
    }
    @Operation(summary = "新增库存", description = "新增库存")
    @PostMapping
    public Result<?> save(@Parameter(description = "库存") @RequestBody WmsStock wmsStock) {
        wmsStockService.saveStock(wmsStock);
        return Result.success();
    }

    @Operation(summary = "修改库存", description = "修改库存")
    @PutMapping
    public Result<?> update(@Parameter(description = "库存") @RequestBody WmsStock wmsStock) {
        wmsStockService.updateStock(wmsStock);
        return Result.success();
    }

    @Operation(summary = "删除库存", description = "删除库存")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(wmsStockService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WmsStock> util = new ExcelUtil<>(WmsStock.class);
        util.importTemplateExcel(response, "库存");
    }

    @Operation(summary = "导出库存列表", description = "导出库存列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStockParam wmsStockParam) {
        WmsStock wmsStock = new WmsStock();
        BeanUtils.copyProperties(wmsStockParam, wmsStock);
        QueryWrapper<WmsStock> wrapper = QueryWrapperUtil.build(wmsStock);
        List<WmsStock> list = wmsStockService.list(wrapper);
        ExcelUtil<WmsStock> util = new ExcelUtil<>(WmsStock.class);
        util.exportExcel(response, list, "库存数据");
    }

    @Operation(summary = "导入库存列表", description = "导入库存列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WmsStock> util = new ExcelUtil<>(WmsStock.class);
        List<WmsStock> wmsStockList = util.importExcel(file.getInputStream());
        String message = wmsStockService.importData(wmsStockList, updateSupport);
        return Result.success(message);
    }
}